/*
 ** Licensed under the Apache License, Version 2.0 (the "License");
 ** you may not use this file except in compliance with the License.
 ** You may obtain a copy of the License at
 **
 **     http://www.apache.org/licenses/LICENSE-2.0
 **
 ** Unless required by applicable law or agreed to in writing, software
 ** distributed under the License is distributed on an "AS IS" BASIS,
 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 ** See the License for the specific language governing permissions and
 ** limitations under the License.
 */

package org.geohunter.activity.map;

import org.geohunter.activity.map.DensityMatrix.DensityPatch;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;

import java.util.ArrayList;
import java.util.List;

public class DensityOverlayDelegate {
    public static final double RESOLUTION_LATITUDE = 0.01;
    public static final double RESOLUTION_LONGITUDE = 0.02;
    public static final int RESOLUTION_LATITUDE_E6 = (int)(RESOLUTION_LATITUDE * 1E6);
    public static final int RESOLUTION_LONGITUDE_E6 = (int)(RESOLUTION_LONGITUDE * 1E6);
    private final Paint mPaint;
    private final Rect mPatchRect;
    private List<DensityPatch> mPatches = new ArrayList<DensityPatch>(0);

    public DensityOverlayDelegate(Rect patchRect, Paint paint) {
        mPatchRect = patchRect;
        mPaint = paint;
    }

    public void setPatches(List<DensityPatch> patches) {
        mPatches = patches;
    }
    
    public void draw(Canvas canvas, 
            int topLatitudeE6, int leftLongitudeE6,
            int bottomLatitudeE6, int rightLongitudeE6,
            Point screenTopLeft, Point screenBottomRight) {
        final double pixelsPerLatE6Degrees = (double)(screenBottomRight.y - screenTopLeft.y)
                / (double)(bottomLatitudeE6 - topLatitudeE6);
        final double pixelsPerLonE6Degrees = (double)(screenBottomRight.x - screenTopLeft.x)
                / (double)(rightLongitudeE6 - leftLongitudeE6);

        for (DensityPatch patch : mPatches) {
            final int patchLatLowE6 = patch.getLatLowE6();
            final int patchLonLowE6 = patch.getLonLowE6();
            int xOffset = (int)((patchLonLowE6 - leftLongitudeE6) * pixelsPerLonE6Degrees);
            int xEnd = (int)((patchLonLowE6 + RESOLUTION_LONGITUDE_E6 - leftLongitudeE6) * pixelsPerLonE6Degrees);
            int yOffset = (int)((patchLatLowE6 - topLatitudeE6) * pixelsPerLatE6Degrees);
            int yEnd = (int)((patchLatLowE6 + RESOLUTION_LATITUDE_E6 - topLatitudeE6) * pixelsPerLatE6Degrees);
            mPatchRect.set(xOffset, yEnd, xEnd, yOffset);
            mPaint.setAlpha(patch.getAlpha());
            canvas.drawRect(mPatchRect, mPaint);
        }
    }

}
